﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Cards.Objects;
using Cards.Util;

namespace Cards.GUI
{
    public partial class INS_Player : UserControl
    {
        protected int x;
        protected int y;
        protected int xStart;
        protected int yStart;
        protected int m_width;
        protected int m_height;
        protected int m_No;                    //thu tu nguoi choi
        protected int m_numOfCards; //so luong quan bai ban dau
        protected int m_maxNumOfCards; //so luong quan bai toi da
        protected Image m_picPath;
        protected PictureBox m_pic;
        protected PictureBox[] m_gPic;
        //private Object m_player;         //CNguoiChoi, CComputer
        protected CPlayer m_player;         //CNguoiChoi, CComputer
        protected INS_Stage m_stage;
        protected INS_Card[] m_cardsArray;
        protected PLAYER_TYPE m_playerKind;        //0: nguoi; 1: computer
        protected int m_sleep = 0;

        protected Label lState = new Label();
        protected Label lAmountOfMoney = new Label();
        protected Label lTitle = new Label();

        //properties
        public int NUMOFCARDS
        {
            get { return m_numOfCards; }
            set { m_numOfCards = value; }
        }
        public int MAXNUMOFCARDS
        {
            get { return m_maxNumOfCards; }
            set { m_maxNumOfCards = value; }
        }
        public int X
        {
            get { return x; }
            set { x = value; }
        }

        public int Y
        {
            get { return y; }
            set { y = value; }
        }
        public int STT
        {
            get { return m_No; }
            set { m_No = value; }
        }
        public int XSTART
        {
            get { return xStart; }
            set { xStart = value; }
        }

        public int YSTART
        {
            get { return yStart; }
            set { yStart = value; }
        }
        public CPlayer PLAYER
        {
            get { return m_player; }
            set { m_player = value; }
        }
        public INS_Stage INSSTAGE
        {
            get { return m_stage; }
            set { m_stage = value; }
        }
        public INS_Card[] IC{
            get { return m_cardsArray; }
            set { m_cardsArray = value; }
        }
        public int WIDTH{
            get { return m_width; }
            set { m_width = value; }
        }
        public int HEIGHT
        {
            get { return m_height; }
            set { m_height= value; }
        }
        public PLAYER_TYPE PLAYERKIND
        {
            get { return m_playerKind; }
            set { m_playerKind = value; }
        }
        public Label STATE
        {
            get { return lState; }
            set { lState = value; }
        }
        public Label MONEY
        {
            get { return lAmountOfMoney; }
            set { lAmountOfMoney = value; }
        }

        public Label TITLE
        {
            get { return lTitle; }
            set { lTitle = value; }
        }

        //Indexers
        public INS_Card this[int index]
        {
            get
            {
                if ((index < 0) || (index > m_cardsArray.Length))
                {
                    return null;
                }
                else
                {
                    return m_cardsArray[index];
                }
            }
            set
            {
                if (index >= 0 && index < m_cardsArray.Length)
                {
                    m_cardsArray[index] = value;
                }
            }
        }

        public INS_Player(int _x, int _y, int _no, String _name, Image _picPath,
                            INS_Stage _stage, PLAYER_TYPE _playerkind, int _width, int _height, 
                            int _xStart, int _yStart, int _numofcards, int _maxnumofcards)
        {
            InitializeComponent();
            x = _x;
            y = _y;
            xStart = _xStart;
            yStart = _yStart;
            m_width = _width;
            m_height = _height;
            m_No = _no;
            m_picPath = _picPath;
            m_stage = _stage;
            m_playerKind = _playerkind;
            m_numOfCards = _numofcards;
            m_maxNumOfCards = _maxnumofcards;

            m_cardsArray = new INS_Card[m_maxNumOfCards];

            Init();
        }

        public void SortCards()
        {
            ArrayUtil.DecreaseSortArray(m_cardsArray);
        }

        public void DeselectAllCards()
        {
            for (int i = 0; i < m_numOfCards; i++)
            {
                if ((m_cardsArray[i] != null) && (!m_cardsArray[i].CARD.PLAY))
                {
                    if (m_cardsArray[i].SELECT == 1)
                    {
                        m_cardsArray[i].SELECT = -1;
                    }
                }
            }
        }

        public void MoveCards()
        {
            for (int i = 0; i < m_numOfCards; i++)
            {
                if ((m_cardsArray[i] != null) && (!m_cardsArray[i].CARD.PLAY))
                {
                    m_cardsArray[i].MoveCard();
                    m_cardsArray[i].BringToFront();
                }
            }
        }

        public virtual void Init()
        {
        }

        public void DeleteCards()
        {
            for (int i = 0; i < m_numOfCards; i++)
            {
                if (m_cardsArray[i] != null)
                {
                    this.Controls.Remove(m_cardsArray[i]);
                    m_cardsArray[i].Dispose();
                }
            }
        }

        public virtual void EndTurn()
        {
            this.m_pic.BorderStyle = BorderStyle.None;
            this.m_pic.BackColor = Color.Transparent;
            if (this.PLAYERKIND == PLAYER_TYPE.HUMAN)
            {
                m_stage.enableButtonPlay(false);
                m_stage.enableButtonSkip(false);
            }
        }
        
        public void HideCards()
        {
            for (int i = 0; i < m_numOfCards; i++)
            {
                m_cardsArray[i].Visible = false;
            }

            //hien hinh la bai up
            for (int i = 0; i < m_numOfCards; i++)
            {
                m_gPic[i].Visible = false;
                m_gPic[i].BringToFront();
            }
        }

        public void DisplayCards()
        {
            for (int i = 0; i < m_numOfCards; i++)
            {
                if (!m_cardsArray[i].CARD.PLAY)
                {
                    m_cardsArray[i].Visible = true;
                    m_cardsArray[i].BringToFront();
                }
            }
            if (m_playerKind == PLAYER_TYPE.COMPUTER)
            {
                for (int i = 0; i < m_numOfCards; i++)
                {
                    m_gPic[i].Visible = false;
                }
            }
        }

        public void DisplayCardAtPosition(int _position)
        {
            m_cardsArray[_position].Visible = true;
            m_cardsArray[_position].BringToFront();
        }

        public void DisplayGCardAtPosition(int _position)
        {
            m_gPic[_position].Visible = true;
            m_gPic[_position].BringToFront();
        }

        public virtual void CalculateMoney(long amount)
        {
        }

        public void AddMoney()
        {
            m_player.AddMoney();
            lAmountOfMoney.Text = m_player.MONEY.ToString();
        }

        /// <summary>
        /// Receiver turn to play
        /// </summary>
        public virtual void ReceiveTurn()
        {
            this.m_pic.BorderStyle = BorderStyle.FixedSingle;
            this.m_pic.BackColor = Color.Aquamarine;
            SetStateText("Đến lượt đi");

            if (MainFrame.s_multiType == MULTI_PLAYER_TYPE.SINGLE_PLAYER)
            {
                if (this.PLAYERKIND == (int)PLAYER_TYPE.HUMAN)
                {
                    m_stage.enableButtonPlay(true);
                    m_stage.enableButtonSkip(true);
                }
            }
            else
            {
                if ((this.PLAYERKIND == (int)PLAYER_TYPE.HUMAN) &&
                    (this.m_No == MainFrame.m_frmMultiGame.GAMEPLAYERID - 1))
                {
                    m_stage.enableButtonPlay(true);
                    m_stage.enableButtonSkip(true);
                }
            }
        }

        public void SetStateText(String _text)
        {
            lState.Text = _text;
        }

        public void ShrinkCards()
        {
            int j = 0;
            for (int i = 0; i < m_numOfCards; i++)
            {
                if (!m_cardsArray[i].CARD.PLAY)
                {
                    m_cardsArray[i].X = Constants.s_avatarWidth +
                                              j * Constants.s_space_bw_two_cards;
                    m_cardsArray[i].MoveCard();
                    j++;
                }
            }
        }

        public void Skip()
        {
            for (int i = 0; i < m_numOfCards; i++)
            {
                if (m_cardsArray[i].SELECT == 1)
                {
                    m_cardsArray[i].SelectCard();
                }
            }
        }

        public int GetRealLength()
        {
            int rs = 0;
            for (int i = 0; i < m_cardsArray.Length; i++)
            {
                if (m_cardsArray[i].CARD.CARDTYPE != -1)
                {
                    ++rs;
                }
            }
            return rs;
        }

        protected virtual void sleepTimer_Tick(object sender, EventArgs e)
        {
        }

        public void HideGPic()
        {
            for (int i = 0; i < m_numOfCards; i++)
            {
                m_gPic[i].Visible = false;
            }
        }
        
        public void AssignCardPictures()
        {
            for (int i = 0; i < m_maxNumOfCards; i++)
            {
                if (m_cardsArray[i].CARD.CARDTYPE == Constants.BABICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._3bich;//"3bich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BACHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._3chuon;// "3chuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BARO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._3ro;// "3ro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BACO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._3co;// "3co.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BONBICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._4bich;// "4bich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BONCHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._4chuon;// "4chuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BONRO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._4ro;// "4ro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BONCO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._4co;// "4co.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.NAMBICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._5bich;// "5bich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.NAMCHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._5chuon;// "5chuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.NAMRO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._5ro;// "5ro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.NAMCO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._5co;// "5co.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.SAUBICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._6bich;// "6bich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.SAUCHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._6chuon;// "6chuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.SAURO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._6ro;// "6ro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.SAUCO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._6co;// "6co.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BAYBICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._7bich;// "7bich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BAYCHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._7chuon;// "7chuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BAYRO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._7ro;// "7ro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BAYCO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._7co;// "7co.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.TAMBICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._8bich;// "8bich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.TAMCHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._8chuon;// "8chuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.TAMRO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._8ro;// "8ro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.TAMCO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._8co;// "8co.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.CHINBICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._9bich;// "9bich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.CHINCHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._9chuon;//"9chuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.CHINRO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._9ro;//"9ro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.CHINCO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._9co;//"9co.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.MUOIBICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._10bich;//"10bich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.MUOICHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._10chuon;//"10chuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.MUOIRO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._10ro;//"10ro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.MUOICO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._10co;// "10co.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BOIBICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.jbich;//"jbich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BOICHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.jchuon;//"jchuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BOIRO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.jro;//"jro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.BOICO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.jco;//"jco.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.DAMBICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.qbich;//"qbich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.DAMCHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.qchuon;//"qchuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.DAMRO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.qro;//"qro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.DAMCO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.qco;//"qco.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.GIABICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.kbich;//"kbich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.GIACHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.kchuon;//"kchuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.GIARO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.kro;//"kro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.GIACO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.kco;//"kco.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.ACHBICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.abich;//"abich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.ACHCHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.achuon;//"achuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.ACHRO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.aro;//"aro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.ACHCO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources.aco;//"aco.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.HAIBICH)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._2bich;//"2bich.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.HAICHUON)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._2chuon;//"2chuon.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.HAIRO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._2ro;//"2ro.png";
                }
                else if (m_cardsArray[i].CARD.CARDTYPE == Constants.HAICO)
                {
                    m_cardsArray[i].PICPATH = global::Cards.Properties.Resources._2co;//"2co.png";
                }
            }
        }
    }
}
